package SingleThreaded;

import java.util.Random;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import GUI.View;



public class SimulationController {
	
	//method that runs the simulation 
		public static JLabel runSim(SimulationBean simBean) {		
			
			long start=System.currentTimeMillis();
			
			String CallPutFlag=simBean.getCallPutFlag();
			Double S=simBean.getS();
			Double X=simBean.getX();
			Double T=simBean.getT();
			Double r=simBean.getR();
			Double b=simBean.getB();
			Double v=simBean.getV();
			int nSteps=simBean.getnSteps();
			int nSimulations=simBean.getnSimulations();
			JLabel result=simBean.getResultat();
			
			Double Dt, St, Drift, vSqrdt;
			Double Sum=0.0;
			int i, j, z=0;
			
			Dt= T/nSteps;
			Drift = (b - (v*v) / 2) * Dt;
			vSqrdt = v * java.lang.Math.sqrt(Dt);
			Random random=new Random();
			
			if(CallPutFlag=="c") {
				z=1;
				System.out.println("call");
			}
			else if (CallPutFlag=="p") {
				z=-1;
			}
			
			for(i=0; i< nSimulations; i++) {
				St=S;
				for(j=0; j<nSteps; j++) {
					St = St * java.lang.Math.exp(Drift + vSqrdt * random.nextGaussian());
				}
				Sum = Sum + java.lang.Math.max (z* (St-X), 0);
			}
			
			long end=System.currentTimeMillis();
			long time=end-start;
			System.out.println(time);
			result.setText("premium" + java.lang.Math.exp(-r*T) * (Sum / nSimulations));
			return(result);
		}
		
		public static void main (String [] args) {
			
			SwingUtilities.invokeLater(new Runnable(){

				public void run(){
					View view=new View();
					view.setVisible(true);	
				}
				
		});
				
		}

}
